package com.jhgsys.internal.incentive.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jhgsys.internal.common.annotation.Log;
import com.jhgsys.internal.common.constant.DictCodeConstant;
import com.jhgsys.internal.common.controller.BaseController;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.exception.FebsException;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.dict.service.IDictService;
import com.jhgsys.internal.incentive.dto.IncentiveRewardClauseRuleDto;
import com.jhgsys.internal.incentive.entity.IncentiveRewardClause;
import com.jhgsys.internal.incentive.entity.IncentiveRewardClauseRule;
import com.jhgsys.internal.incentive.service.IIncentiveRewardClauseRuleRecordService;
import com.jhgsys.internal.incentive.service.IIncentiveRewardClauseRuleService;
import com.jhgsys.internal.incentive.service.IIncentiveRewardClauseService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import javax.validation.constraints.NotBlank;
import java.util.Date;
import java.util.Objects;

/**
 * 奖励条款规则 Controller
 *
 * @author zjb
 * @date 2024-08-30 09:51:15
 */
@Slf4j
@Validated
@Controller
public class IncentiveRewardClauseRuleController extends BaseController {

    @Autowired
    private IIncentiveRewardClauseRuleService incentiveRewardClauseRuleService;

    @Autowired
    private IIncentiveRewardClauseService incentiveRewardClauseService;

    @Autowired
    private IDictService dictService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "incentiveRewardClauseRule")
    @RequiresPermissions("incentiveRewardClauseRule:view")
    public String incentiveRewardClauseRuleIndex(Model model) {
        model.addAttribute("hasHeadCompanyRole", hasHeadCompanyRole());
        model.addAttribute("tenantList", getTenantList());
        model.addAttribute("currentTenantId", getCurrentTenantId());
        model.addAttribute("disposeFormList", dictService.findDictByCode(DictCodeConstant.SAFETY_PECCANCY_DISPOSE_FORM));
        return FebsUtil.view("incentive/incentiveRewardClauseRule/incentiveRewardClauseRuleList");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "incentiveRewardClauseRule/add")
    @RequiresPermissions("incentiveRewardClauseRule:add")
    public String incentiveRewardClauseRuleAdd() {
        return FebsUtil.view("incentive/incentiveRewardClauseRule/incentiveRewardClauseRuleEdit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "incentiveRewardClauseRule/update/{id}")
    @RequiresPermissions("incentiveRewardClauseRule:edit")
    public String incentiveRewardClauseRuleUpdate(Model model, @PathVariable("id") String id) {
        IncentiveRewardClauseRule incentiveRewardClauseRuleById = incentiveRewardClauseRuleService.findIncentiveRewardClauseRuleById(id);
        IncentiveRewardClause incentiveRewardClauseById = incentiveRewardClauseService.findIncentiveRewardClauseById(incentiveRewardClauseRuleById.getClauseId());
        IncentiveRewardClauseRuleDto incentiveRewardClauseRuleDto = new IncentiveRewardClauseRuleDto();
        BeanUtils.copyProperties(incentiveRewardClauseRuleById, incentiveRewardClauseRuleDto);
        incentiveRewardClauseRuleDto.setClauseName(incentiveRewardClauseById.getClauseName());
        model.addAttribute("incentiveRewardClauseRule", incentiveRewardClauseRuleDto);
        return FebsUtil.view("incentive/incentiveRewardClauseRule/incentiveRewardClauseRuleEdit");
    }

    /**
     * 查询（所有）
     *
     * @author zjb
     * @date 2024-08-30 09:51:15
     */
    @GetMapping("incentiveRewardClauseRule")
    @ResponseBody
    @RequiresPermissions("incentiveRewardClauseRule:view")
    public FebsResponse getAllIncentiveRewardClauseRules(IncentiveRewardClauseRule incentiveRewardClauseRule) {
        return new FebsResponse().success().data(incentiveRewardClauseRuleService.findIncentiveRewardClauseRuleList(incentiveRewardClauseRule));
    }

    /**
     * 查询（分页）
     *
     * @author zjb
     * @date 2024-08-30 09:51:15
     */
    @GetMapping("incentiveRewardClauseRule/list")
    @ResponseBody
    @RequiresPermissions("incentiveRewardClauseRule:view")
    public FebsResponse incentiveRewardClauseRuleList(QueryRequest request, IncentiveRewardClauseRule incentiveRewardClauseRule) {
        Map<String, Object> dataTable = getDataTable(this.incentiveRewardClauseRuleService.findIncentiveRewardClauseRulePage(request, incentiveRewardClauseRule));
        return new FebsResponse().success().data(dataTable);
    }

    /**
     * 新增 IncentiveRewardClauseRule
     *
     * @author zjb
     * @date 2024-08-30 09:51:15
     */
    @Log("新增IncentiveRewardClauseRule")
    @PostMapping("incentiveRewardClauseRule")
    @ResponseBody
    @RequiresPermissions("incentiveRewardClauseRule:add")
    public FebsResponse addIncentiveRewardClauseRule(@RequestBody @Valid IncentiveRewardClauseRule incentiveRewardClauseRule) throws FebsException {
        try {
            IncentiveRewardClauseRule rule = new IncentiveRewardClauseRule();
            rule.setClauseNo(incentiveRewardClauseRule.getClauseNo());
            List<IncentiveRewardClauseRule> incentiveRewardClauseRuleList = this.incentiveRewardClauseRuleService.findIncentiveRewardClauseRuleList(rule);
            if (!CollectionUtils.isEmpty(incentiveRewardClauseRuleList)) {
                return new FebsResponse().fail().message("条款书编号已存在！");
            }
            incentiveRewardClauseRule.setCreatedBy(getCurrentUser().getUserName());
            this.incentiveRewardClauseRuleService.createIncentiveRewardClauseRule(incentiveRewardClauseRule);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "新增IncentiveRewardClauseRule失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 逻辑删除 IncentiveRewardClauseRule
     *
     * @author zjb
     * @date 2024-08-30 09:51:15
     */
    @Log("逻辑删除IncentiveRewardClauseRule")
    @GetMapping("incentiveRewardClauseRule/delete/{id}")
    @ResponseBody
    @RequiresPermissions("incentiveRewardClauseRule:delete")
    public FebsResponse deleteIncentiveRewardClauseRule(@PathVariable("id") String incentiveRewardClauseRuleIds) throws FebsException {
        try {
            IncentiveRewardClauseRule incentiveRewardClauseRule = new IncentiveRewardClauseRule();
            incentiveRewardClauseRule.setDeletedBy(getCurrentUser().getUserName());
            incentiveRewardClauseRule.setDeletedTime(new Date());
            incentiveRewardClauseRule.setDeletedFlg(FebsConstant.INVALID);
            this.incentiveRewardClauseRuleService.deleteIncentiveRewardClauseRule(incentiveRewardClauseRule, incentiveRewardClauseRuleIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除IncentiveRewardClauseRule失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 物理删除 IncentiveRewardClauseRule
     *
     * @author zjb
     * @date 2024-08-30 09:51:15
     */
    @Log("物理删除IncentiveRewardClauseRule")
    @GetMapping("incentiveRewardClauseRule/physicallyDelete/{id}")
    @ResponseBody
    @RequiresPermissions("incentiveRewardClauseRule:delete")
    public FebsResponse physicallyDeleteIncentiveRewardClauseRule(@PathVariable("id") String incentiveRewardClauseRuleIds) throws FebsException {
        try {
            this.incentiveRewardClauseRuleService.physicallyDeleteIncentiveRewardClauseRule(incentiveRewardClauseRuleIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除IncentiveRewardClauseRule失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 修改 IncentiveRewardClauseRule
     *
     * @author zjb
     * @date 2024-08-30 09:51:15
     */
    @Log("修改IncentiveRewardClauseRule")
    @PostMapping("incentiveRewardClauseRule/update")
    @ResponseBody
    @RequiresPermissions("incentiveRewardClauseRule:edit")
    public FebsResponse updateIncentiveRewardClauseRule(@RequestBody @Valid IncentiveRewardClauseRule incentiveRewardClauseRule) throws FebsException {
        try {
            IncentiveRewardClauseRule rule = new IncentiveRewardClauseRule();
            rule.setClauseNo(incentiveRewardClauseRule.getClauseNo());
            List<IncentiveRewardClauseRule> incentiveRewardClauseRuleList = this.incentiveRewardClauseRuleService.findIncentiveRewardClauseRuleList(rule);
            if (!CollectionUtils.isEmpty(incentiveRewardClauseRuleList) && !Objects.equals(incentiveRewardClauseRule.getClauseId(),incentiveRewardClauseRuleList.get(0).getClauseId())) {
                return new FebsResponse().fail().message("条款书编号已存在！");
            }
            incentiveRewardClauseRule.setUpdatedBy(getCurrentUser().getUserName());
            this.incentiveRewardClauseRuleService.updateIncentiveRewardClauseRule(incentiveRewardClauseRule);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "修改IncentiveRewardClauseRule失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 修改 PeccancyClauseRule
     * @author admin
     * @date 2024-08-08 13:20:28
     */
    @Log("修改incentiveRewardClauseRule")
    @PostMapping("incentiveRewardClauseRule/updateStatus")
    @ResponseBody
    @RequiresPermissions("incentiveRewardClauseRule:edit")
    public FebsResponse updateStatusPeccancyClauseRule(@RequestBody IncentiveRewardClauseRule incentiveRewardClauseRule) throws FebsException {
        try {
            if(StringUtils.isBlank(incentiveRewardClauseRule.getClauseRuleId())){
                return new FebsResponse().fail().message("条款书ID不能为空");
            }
            if(StringUtils.isBlank(incentiveRewardClauseRule.getStatus())){
                return new FebsResponse().fail().message("条款状态不能为空");
            }
            incentiveRewardClauseRule.setUpdatedBy(getCurrentUser().getUserName());
            incentiveRewardClauseRule.setInsertFlg(false);
            this.incentiveRewardClauseRuleService.updateIncentiveRewardClauseRule(incentiveRewardClauseRule);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "修改incentiveRewardClauseRule失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

}
